import {
  formatNumbers
} from '../../utils/util'
Page({
  data: {
    // 编辑完成切换,true 编辑   false 完成
    cutChange: true,
    checkNum: 0,
    countMoney: '0.00',
    orderidArr: [],
    checkAll: "all",
    // 全选初始化
    checkAllObj: {
      checks: false
    },
    items: [{
        orderId: '1',
        imgUrl: 'https://img14.360buyimg.com/n0/jfs/t1/157056/28/6/67298/5fe94ff9E91bcfc7f/26cd4a19b1ae69b2.jpg',
        shopName: '1期免息【高配1799】Xiaomi/小米 小米NOTE 3全网通小米手机新品 128G',
        shopInfo: '简单的描述',
        price: '100.20',
        userNum: 3,
        checks: false,
      },
      {
        orderId: '2',
        imgUrl: 'https://img14.360buyimg.com/n0/jfs/t1/157056/28/6/67298/5fe94ff9E91bcfc7f/26cd4a19b1ae69b2.jpg',
        shopName: '2期免息【高配2499】Xiaomi/小米 小米NOTE 3全网通小米手机新品 128G',
        shopInfo: '简单的描述',
        price: '100.74',
        userNum: 3,
        checks: false
      },
      {
        orderId: '3',
        imgUrl: 'https://img14.360buyimg.com/n0/jfs/t1/157056/28/6/67298/5fe94ff9E91bcfc7f/26cd4a19b1ae69b2.jpg',
        shopName: '3期免息【高配2799】Xiaomi/小米 小米NOTE 3全网通小米手机新品 128G',
        shopInfo: '简单的描述',
        price: '100.00',
        userNum: 3,
        checks: false
      },
      {
        orderId: '4',
        imgUrl: 'https://img14.360buyimg.com/n0/jfs/t1/157056/28/6/67298/5fe94ff9E91bcfc7f/26cd4a19b1ae69b2.jpg',
        shopName: '4期免息【高配12799】Xiaomi/小米 小米NOTE 3全网通小米手机新品 128G',
        shopInfo: '简单的描述',
        price: '100.00',
        userNum: 3,
        checks: false
      },
      {
        orderId: '5',
        imgUrl: 'https://img14.360buyimg.com/n0/jfs/t1/157056/28/6/67298/5fe94ff9E91bcfc7f/26cd4a19b1ae69b2.jpg',
        shopName: '5期免息【高配9799】Xiaomi/小米 小米NOTE 3全网通小米手机新品 128G',
        shopInfo: '简单的描述',
        price: '100.00',
        userNum: 3,
        checks: false
      },
      {
        orderId: '6',
        imgUrl: 'https://img14.360buyimg.com/n0/jfs/t1/157056/28/6/67298/5fe94ff9E91bcfc7f/26cd4a19b1ae69b2.jpg',
        shopName: '6期免息【高配2099】Xiaomi/小米 小米NOTE 3全网通小米手机新品 128G',
        shopInfo: '简单的描述',
        price: '100.00',
        userNum: 3,
        checks: false
      }
    ]
  },
  checkboxChange(e) {
    let arr = this.data.items;
    if (e.detail.value[0] == "all") { //全选、全不选
      if (e.target.dataset.checkall == "all") {
        let monery = 0;
        // 所有购物车中商品id
        let shopids = [];
        for (var i = 0; i < arr.length; i++) {
          arr[i].checks = true;
          this.data.orderidArr.push(arr[i].orderId);
          monery += arr[i].price * arr[i].userNum;
          shopids[i] = arr[i].orderId;
        }

        this.setData({
          items: arr,
          checkNum: arr.length,
          countMoney: formatNumbers(monery),
          orderidArr: shopids
        });
      }
    } else {
      if (e.currentTarget.dataset.checkall == 'all') {
        let monery = 0;
        for (var i = 0; i < arr.length; i++) {
          arr[i].checks = false;
          this.data.orderidArr.push(arr[i].orderId);
        }
        let checkss = this.data.checkAllObj;
        checkss.checks = false;
        this.setData({
          items: arr,
          checkNum: 0,
          countMoney: '0.00',
          orderidArr: [],
          checkAllObj: checkss
        });
      } else {
        let checkss = this.data.checkAllObj;
        if (e.detail.value.length == 0) {
          checkss.checks = false;
        } else if (e.detail.value.length == this.data.items.length) {
          checkss.checks = true;
        }

        this.setData({
          checkNum: e.detail.value.length,
          checkAllObj: checkss
        });
      }
    }



  },
  checkboxChangeRedact(event) {
    let shopPrice = event.currentTarget.dataset.price;
    let usernum = event.currentTarget.dataset.usernum;

    if (this.data.orderidArr.indexOf(event.currentTarget.dataset.orderid) == -1) {
      // 总价格
      this.setData({
        countMoney: formatNumbers(parseFloat(this.data.countMoney) + shopPrice * usernum)
      });
    } else if (this.data.orderidArr.indexOf(event.currentTarget.dataset.orderid) != -1) {
      // 总价格
      this.setData({
        countMoney: formatNumbers(parseFloat(this.data.countMoney) - shopPrice * usernum)
      });
    }
    // 判断数组中的id是否存在， 存在的话就删除
    if (this.data.orderidArr.indexOf(event.currentTarget.dataset.orderid) == -1) {
      this.data.orderidArr.push(event.currentTarget.dataset.orderid);
    } else {
      if (this.data.orderidArr.length != 0) {
        // 改变数据中的选中、、checked
        let arrS = this.data.items;
        for (let i = 0; i < arrS.length; i++) {
          if (event.currentTarget.dataset.orderid == arrS[i].orderId) {
            arrS[i].checks = false;
          }
        }
        this.setData({
          items: arrS
        });
      }
      this.data.orderidArr.splice(this.data.orderidArr.indexOf(event.currentTarget.dataset.orderid), 1);
    }
    // 改变数据默认
    if (this.data.orderidArr.length != 0) {
      // 改变数据中的选中、、checked
      let arrS = this.data.items;
      for (let h = 0; h < this.data.orderidArr.length; h++) {
        for (let i = 0; i < arrS.length; i++) {
          if (this.data.orderidArr[h] == arrS[i].orderId) {
            arrS[i].checks = true;
          }
        }
      }
      this.setData({
        items: arrS
      });
    }
  },
  // 加减
  plusNum(event) {
    // 改变数量
    if (event.target.dataset.identifying == 'plus') {
      let arrplus = this.data.items;
      for (var i = 0; i < arrplus.length; i++) {
        if (arrplus[i].orderId == event.target.dataset.orderid) {
          if (arrplus[i].userNum != 999) {
            arrplus[i].userNum++;
          } else {
            wx.showToast({
              title: '您已经超过最小购买数量！',
              icon:none
            })
          }
        }
      }
      this.setData({
        items: arrplus
      });
    } else if (event.target.dataset.identifying == 'subtract') {
      let arrplus = this.data.items;
      for (var i = 0; i < arrplus.length; i++) {
        if (arrplus[i].orderId == event.target.dataset.orderid) {
          if (arrplus[i].userNum != 1) {
            arrplus[i].userNum--;
          } else {
            wx.showToast({
              title: '您已经超过最小购买数量！',
              icon:"none"
            })
          }
        }
      }
      this.setData({
        items: arrplus
      });
    }
    // 给选中的商品改变总金额数
    if (this.data.orderidArr.length != 0) {
      var moneyCount = null;
      for (let i = 0; i < this.data.orderidArr.length; i++) {
        for (let j = 0; j < this.data.items.length; j++) {
          if (this.data.orderidArr[i] == this.data.items[j].orderId) {
            moneyCount += this.data.items[j].price * this.data.items[j].userNum;
          }
        }
      }
      // 总金额
      this.setData({
        countMoney: formatNumbers(moneyCount)
      })
    }
  },
  // 订单详情
  orderdetali(event) {
    console.log("这里是详情页面！");
  },
  // 清除购物车中失效的商品
  clearData(event) {
    let dataArr = this.data.items;
    for (let i = 0; i < dataArr.length; i++) {
      if (event.target.dataset.orderid == dataArr[i].orderId) {
        dataArr.splice(i, 1);
      }
    }
    this.setData({
      items: dataArr
    });
  },
  // 切换编辑与移除
  cutStatus(event) {
    if (event.target.dataset.cutchanges) {
      this.setData({
        cutChange: false
      });
    } else {
      this.setData({
        cutChange: true
      });
    }
    this.setData({
      countMoney:"0.00"
    })
  },
  // 选中移除
  removeData(event) {
    let dataArr = this.data.items;
    if (this.data.orderidArr.length != 0) {
      // 移除数据
      for (let j = 0; j < this.data.orderidArr.length; j++) {
        for (let i = 0; i < dataArr.length; i++) {
          if (this.data.orderidArr[j] == dataArr[i].orderId) {
            dataArr.splice(i, 1);
          }
        }
      }
      this.setData({
        items: dataArr,
        orderidArr: [],
        checkNum: 0
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})